//----------------------------------------------------------------------------
// askcmd.cpp
//
// ask questions via message boxes
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const ASK_CMD 		= "ask";
const char * const CAPTION_OPT 	= "caption";
const char * const TEXT_OPT 	= "text";
const char * const ICON_OPT		= "icon";
const char * const BUTT_OPT		= "buttons";

const char * const ASK_HELP =
	"ask question via message box\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Swine') \n"
	" --text        supply text for message box, if not used, read standard input\n"
	" --icon        icon to use - one of: info, query (default), warn or stop\n"
	" --buttons     buttons to use - one of yesno (default), okcancel, yesnocancel, retrycancel\n";

//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class AskCmd : public Command {

	public:

		AskCmd() : Command( ASK_CMD, ASK_HELP ) {}

		int Execute( CmdLine & cl ) const;
};

static AddCommand <AskCmd> cmd1_;

//----------------------------------------------------------------------------
// If the text option is not specified, read lines from the command line and
// use those. The reply is the textual name of the button that was presed.
//----------------------------------------------------------------------------

int AskCmd :: Execute( CmdLine & cl ) const {

	string caption = cl.ExtractOption( CAPTION_OPT, "Swine" );
	string text = cl.ExtractOption( TEXT_OPT, "" );
	string icon = cl.ExtractOption( ICON_OPT, "query" );
	string butt = cl.ExtractOption( BUTT_OPT, "yesno" );
	cl.MustBeEmpty();

	if ( text == "" ) {
		text = ReadLines( std::cin );
	}

	int rv = MessageBox( 0, text.c_str(), caption.c_str(),
					IconFromName( icon ) + ButtonsFromName( butt ) );

	Reply( ButtonToString( rv ) );

	return 0;
}

//---------------------------------------------------------------------------

}	//  namespace



